﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess.Repository;
using DataAccess.Repository.Interface;
using DataAccess.Models;

namespace KPI
{
    public partial class AddDynamicWork : BasePage
    {
        private IDynamicWorkRepository idwr = new DynamicWorkRepository();
        protected int WorkNewID
        {
            get
            {
                return CommonHelper.QueryStringInt("id");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var u = KPISiteContext.Current.CurrentUser;
                if (u != null && u.RoleName != "administrators")
                {
                    Response.Redirect("~/DynamicWorksList.aspx");
                }

                if (WorkNewID > 0)
                {
                    btnReturn.Visible = true;
                    WorkNews wnDetail = idwr.GetDetailWorkNews(WorkNewID);

                    if (wnDetail != null)
                    {
                        this.pdAddtime.Value = wnDetail.WorkNewsAddTime.Value.ToString();
                        this.WorkNewTitle.Text = wnDetail.WorkNewsTitle;
                        this.WorkNewContent.Text = wnDetail.WorkNewsContent;
                    }
                }

            }

        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (WorkNewID > 0)
            {
                Title = "编辑工作动态";
                lTitle.Text = "编辑工作动态";
            }
            else
            {
                Title = "添加工作动态";
                lTitle.Text = "添加工作动态";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            WorkNews wn = new WorkNews();
            if (WorkNewID > 0)
            {
                wn.WorkNewsID = WorkNewID;
                wn.WorkNewsAddTime = Convert.ToDateTime(this.pdAddtime.Value);
                wn.WorkNewsModifyTime = DateTime.Now;
                wn.WorkNewsTitle = this.WorkNewTitle.Text;
                wn.WorkNewsContent = this.WorkNewContent.Text;
                idwr.EditWorkNews(wn);
                Response.Redirect("~/DynamicWorkDetail.aspx?wnid=" + WorkNewID.ToString());
            }
            else
            {
                wn.WorkNewsAddTime = DateTime.Now;
                wn.WorkNewsModifyTime = DateTime.Now;
                wn.WorkNewsTitle = this.WorkNewTitle.Text;
                wn.WorkNewsContent = this.WorkNewContent.Text;
                idwr.CreateWorkNews(wn);
                Response.Redirect("~/DynamicWorksList.aspx");
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/DynamicWorkDetail.aspx?wnid=" + WorkNewID.ToString());
        }
    }
}